<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\admin\model\StoreJoinActivity;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class withdraw extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Withdraw();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['store_info']) &&   $param['store_info']){
            $where[] = ['storeInfo.store_name|storeInfo.telphone','like',"%{$param['store_info']}%"];
        }
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin(['platformInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin(['platformInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){
          //  $list[$key]['create_time']  = date('Y-m-d H:i',$value['create_time']);
        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }



    public function edit(){
        $request = $this->request;
        $param = $request->param();
       // $activityModel = new \app\admin\model\Activity();
        $data = [
            'status' => $param['status'],
        ];
        if (isset($param['reason']) && $param['reason']){
            $data['reason'] = $param['reason'];
        }

            $withdraw = new \app\admin\model\template\Withdraw();

            $info = $this->model->where(['id' => $param['id']])->find();
            $storeModel = new \app\admin\model\Store();
            $storeInfo = $storeModel->where(['id'=> $info['store_id']])->find();
            $userModel = new \app\admin\model\User();
            $userInfo  = $userModel->where(['id' => $storeInfo['uid']])->find();

            $platformModel  = new \app\admin\model\Platform();
            $platformInfo =  $platformModel->where(['id' => $info['platform_id']])->find();
            $templateModel   = new \app\admin\model\Template();
            $templateInfo = $templateModel->where(['name'=>'withdraw_result_template',
                'platform_id'=> $info['platform_id'] ])->find();
            $sendData = [
                'template_id' => $templateInfo['template_id'],
                'appid' => $platformInfo['xcx_appid'],
                'appsecret' => $platformInfo['xcx_appsecret'],
                'openid' => $userInfo['openid'],
                'apply_amount' => $info['apply_amount'],
                'amount' => $info['amount'],

            ];
            if ($param['status'] ==2 ){
                $storeModel->where(['id'=> $info['store_id']])
                    ->inc('withdrawn',$info['apply_amount'])
                ->dec('freeze',$info['apply_amount'])->update();
                $sendData['result'] ='通过';
            }else{

                $storeModel->where(['id'=> $info['store_id']])
                    ->inc('balance',$info['apply_amount'])
                    ->dec('freeze',$info['apply_amount'])->update();
                $sendData['result'] ='驳回';
            }
            $withdraw->send($sendData);

        $this->model->where(['id' => $param['id']])->update($data);
        return  msg([], 0, "保存成功") ;
    }








}